package com.mytest.test.controller;

import com.mytest.test.entity.Students;
import com.mytest.test.mapper.StudentsBatchMapper;
import com.mytest.test.service.impl.StudentsBatchServiceImpl;
import com.mytest.test.service.impl.StudentsServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description: 测试批量插入
 * @author: chengxucai
 * @createDate: 2022/03/14
 *http://localhost:8090/doc.html#/home
 *
 */
@RestController
@RequestMapping("/student")
@Api(value = "StudentsController测试", tags = "StudentsController测试")
public class StudentsController {

    @Resource
    private StudentsServiceImpl studentsServiceImpl;
    @Resource
    private StudentsBatchServiceImpl studentsBathServiceImpl;
    @Resource
    private StudentsBatchMapper studentsBathMapper;



    /**
     * 联合主键通过service批量插入
     */
    @GetMapping("/batchInsertByService")
    @ApiOperation(value = "联合主键通过service批量插入")
    public void batchInsertByService() {
        List<Students> list=new ArrayList<>();
        Students students1 =new Students(2,2,"李四","青羊区",new Date());
        Students students2 =new Students(2,3,"王五","高新区",new Date());
        list.add(students1);
        list.add(students2);
        studentsBathServiceImpl.saveOrUpdateBatch(list);
    }

    /**
     * 联合主键通过mapper批量插入
     */
    @GetMapping("/batchInsertByMapper")
    @ApiOperation(value = "联合主键通过mapper批量插入")
    public void batchInsertByMapper() {
        List<Students> list=new ArrayList<>();
        Students students1 =new Students(2,2,"李四","武侯区",new Date());
        Students students2 =new Students(2,3,"王五","成华区",new Date());
        list.add(students1);
        list.add(students2);
        studentsBathMapper.mysqlInsertOrUpdateBath(list);
    }

    /**
     * 普通单主键批量插入
     */
    @GetMapping("/batchInsertByOrdinary")
    @ApiOperation(value = "普通单主键批量插入")
    public void batchInsertByOrdinary() {
        List<Students> list=new ArrayList<>();
        Students students1 =new Students(2,2,"李四","锦江区",new Date());
        Students students2 =new Students(2,3,"王五","金牛区",new Date());
        list.add(students1);
        list.add(students2);
        studentsServiceImpl.saveOrUpdateBatch(list);
    }


}
